<?php
/*
 * @Author: your name
 * @Date: 2020-06-29 09:28:29
 * @LastEditTime: 2020-08-13 13:29:36
 * @LastEditors: 天羽落星辰
 * @Description: In User Settings Edit
 * @FilePath: \WWW\frx\application\api\controller\Index.php
 */ 

namespace app\api\controller;

use app\common\controller\Api;
use app\api\controller\Contract;


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index','quotation','setting','version'];
    // protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {   
        $data = [];
        // 轮播图
        $chart = db('chart')->field('img')->where('status','normal')->where('category',0)->order('updatetime desc')->select();
        // 最新公告通知
        $notify = db('notice')->field('id, title')->where('status','normal')->where('sort_id','14')->where('pmd',1)->order('updatetime', 'desc')->order('id', 'desc')->find();
        // 广告位
        $ad = db('chart')->field('img,notice_id')->where('category',1)->where('status','normal')->limit(2)->select();
        if($chart){
            foreach ($chart as &$v) {
                $v['img'] = request()->domain().$v['img'];
            }
        }

        if($ad){
            foreach ($ad as &$v) {
                $v['img'] = request()->domain().$v['img'];
            }
        }
        $data['chart'] = $chart;
        $data['notify'] = $notify ? $notify : '';
        $data['ad'] = $ad;
        // $data['pool_money'] = round(config('site.pool_money'),4);
        // 合约理财
        $data['contract'] = Contract::index();
        $this->success('',$data);
    }
    
    /**
     * 行情
     *
     */
    public function quotation()
    {
        $quotation = moneyprice();
        $this->success('',$quotation);
    }

    /**
     * 系统参数
     */
    public function setting()
    {   
        $this->success('',config('site'));
    }


    
    /**
     * 版本号
     *
     * @param string $version  版本
     */
    public function version()
    {
        $version = $this->request->request('version');
        if(!$version){
            $this->error('版本号不能为空');
        }
        $data = db('versions')->field('version, state,description,url')
        ->where('version','>',$version)->where('state',2)->find();
        
        if($data){
            $data['url'] = request()->domain().$data['url'];
        }else{
            $this->error('不需要更新');
        }
        $this->success('',$data);
        
    }

    


    /**
     * 红包
     */
    public function red_packet()
    {   

        $config = config('site');

        //如果已经有账号则直接登录
        // $ret = $this->auth->direct(2);
        $user = $this->auth->getUser();

        // $users_count = db('user')->where('tid',$user['id'])->where('usdt','>=',10)->where('order_sum','>',0)->count();
        // if(!$users_count){
        //     $this->error('没有满足条件无法抢购红包');
        // }
        $orders_count = db('qianbao_sysetm_log')->where('uid',$user['id'])->where('type',10)->whereTime('createtime','today')->count();

        $users_count = db('user')->where('tid',$user['id'])->whereTime('createtime','today')->count();
        
        if($orders_count >= $config['frequency'] + $users_count*$config['frequency']){
            $this->error('今日次数已用完');
            // $this->error('今日次数已用完');
        }

        $type = array_rand($config['currency_number'],1);
        $money = $config['currency_number'][$type];
        if($type == 'ETH' || $type == 'BTC'){
           db('qianbao_sysetm_log')->insert([
                'uid' => $user['id'],
                'uname' => $user['username'],
                'bizhong' => strtolower($type),
                'money' => $money,
                // 'before' => $user['tcc'],
                // 'after' => $user['tcc'] + $row['get_tcc'],
                'type' => 10,
                'zt' => 1,
                'memo' => '恭喜您抢到'.$money.$type,
                'createtime' => time(),
            ]);
        }else{
            db('qianbao_sysetm_log')->insert([
                'uid' => $user['id'],
                'uname' => $user['username'],
                'bizhong' => strtolower($type),
                'money' => $money,
                'before' => $user[strtolower($type)],
                'after' => $user[strtolower($type)] + $money,
                'type' => 10,
                'zt' => 1,
                'memo' => '恭喜您抢到'.$money.$type,
                'createtime' => time(),
            ]);

            db('user')->where('id',$user['id'])->setInc(strtolower($type),$money);
        }

        $this->success('恭喜您抢到'.$money. $type);
    }
    
   
}
